package com.neutech.dao.impl;

import com.neutech.dao.AdminDAO;
import com.neutech.entity.admin;
import com.neutech.entity.business;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class adminAchieve implements AdminDAO {
    Connection connection = databaseConnection.getConnection();

    static PreparedStatement ps = null;
    static ResultSet rs = null;

    static admin admin = new admin();
    @Override
    public List<admin> listAdmin() {
        int count = 0;
        List<admin> list = new ArrayList<>();
        try{
            ps = connection.prepareStatement("select * from admin");
            rs = ps.executeQuery();
            while (rs.next()){
                admin.setAdminId(rs.getInt("adminId"));
                admin.setAdminName(rs.getString(("adminName")));
                admin.setPassword(rs.getString(("password")));
                list.add(admin);
                count++;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    @Override
    public int getAdminByIdByPass(Integer Id, String Pass) {
        int count = 0;
        try {
            ps = connection.prepareStatement("select * from admin where adminId = ? and password = ?");

            ps.setInt(1,Id);
            ps.setString(2,Pass);

            rs = ps.executeQuery();
            if(rs.next()){
                count++;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

//    @Override
//    public int registerAdmin(Integer Id, String Pass) {
//        int count = 0;
//        try{
//            ps = connection.prepareStatement("insert into admin(adminId,password) values (?,?)");
//            ps.setInt(1,Id);
//            ps.setString(2,Pass);
//
//            ps.executeUpdate();
//            count++;
//        } catch (SQLException e) {
//            throw new RuntimeException(e);
//        }
//
//        return count;
//    }
//
//    @Override
//    public int getAdminById(Integer Id) {
//        int count = 0;
//        try{
//            ps = connection.prepareStatement("select * from amdin where adminId = ?");
//            ps.setInt(1,Id);
//
//            rs = ps.executeQuery();
//            while (rs.next()){
//                admin.setAdminId(rs.getInt("adminId"));
//                admin.setAdminName(rs.getString(("adminName")));
//                admin.setPassword(rs.getString(("password")));
//                System.out.println(admin);
//                count++;
//            }
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        return count;
//    }

    @Override
    public int addAdmin(admin Admin) {
        int count = 0;
        try{
            ps = connection.prepareStatement("insert into admin(adminId,adminName,password) values(?,?,?)");
            ps.setInt(1,admin.getAdminId());
            ps.setString(2,admin.getAdminName());
            ps.setString(3,admin.getPassword());

            ps.executeUpdate();
            count++;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

//    @Override
//    public int updateAdmin(admin Admin) {
//        int count = 0;
//        try{
//            ps = connection.prepareStatement("update admin set adminId = ?,adminName = ?,password = ?");
//            ps.setInt(1,admin.getAdminId());
//            ps.setString(2,admin.getAdminName());
//            ps.setString(3,admin.getPassword());
//
//
//            ps.executeUpdate();
//            count++;
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        return count;
//    }
//
//    @Override
//    public int deleteAdmin(admin Admin) {
//        int count = 0;
//        try{
//            ps = connection.prepareStatement("delete from admin where adminName = ?");
//            ps.setString(1,admin.getAdminName());
//
//            ps.executeUpdate();
//            count++;
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        return count;
//    }

}
